﻿namespace TfsCloudDeploy.Workflow.Core
{
    using System;
    using System.ComponentModel;
    using Microsoft.TeamFoundation.VersionControl.Client;
    using TfsCloudDeploy.Workflow.Core.Activities;

    /// <summary>
    /// Stores metadata about custom server mappings that are typically used to override the default workspace mapping of a build definition.
    /// </summary>
    [Serializable, TypeConverter(typeof(VersionedServerMappingConverter))]
    public class VersionedServerMapping
    {
        private const string DefaultBaselineVersion = "T";

        public VersionedServerMapping()
            : this(string.Empty, string.Empty)
        {
        }

        public VersionedServerMapping(string localItem, string serverItem)
            : this(localItem, serverItem, WorkingFolderType.Map)
        {
        }

        public VersionedServerMapping(string localItem, string serverItem, WorkingFolderType workingFolderType)
        {
            this.LocalItem = localItem;
            this.ServerItem = serverItem;
            this.MappingType = workingFolderType;
            this.BaselineVersion = DefaultBaselineVersion;
        }

        /// <summary>
        /// Specifies the mapping type for the item (Map or Cloak).
        /// </summary>
        [Browsable(true), RefreshProperties(RefreshProperties.All), DefaultValue(WorkingFolderType.Map), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.MappingTypeDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.MappingTypeDescription)]
        public WorkingFolderType MappingType { get; set; }

        /// <summary>
        /// Specifies the local item to which a server item is mapped.
        /// </summary>
        [Browsable(true), RefreshProperties(RefreshProperties.All), DefaultValue(""), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.LocalItemDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.LocalItemDescription)]
        public string LocalItem { get; set; }

        /// <summary>
        /// Specifies the server item for which a mapping is being defined.
        /// </summary>
        [Browsable(true), RefreshProperties(RefreshProperties.All), DefaultValue("$/"), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.ServerItemDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.ServerItemDescription)]
        public string ServerItem { get; set; }

        /// <summary>
        /// Specifies the baseline version to be used for the defined server item.
        /// </summary>
        [Browsable(false), RefreshProperties(RefreshProperties.All), DefaultValue("T"), SRDisplayName(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.BaselineVersionDisplayName), SRDescription(typeof(ExtendedWorkflowResources), ExtendedWorkflowResources.BaselineVersionDescription)]
        public string BaselineVersion { get; set; }

        public override string ToString()
        {
            return string.Format("[{0}] {1} ({2})",
                this.MappingType == WorkingFolderType.Map ? "Active" : "Cloaked",
                this.ServerItem,
                this.BaselineVersion.ToLowerInvariant() == "t" ? "Latest" : this.BaselineVersion);
        }
    }
}
